package com.zpark.resume.service.impl;

import com.zpark.resume.entity.Education;
import com.zpark.resume.entity.Experience;
import com.zpark.resume.exception.AddExperienceLoseException;
import com.zpark.resume.exception.DeleteExperienceLoseException;
import com.zpark.resume.exception.FindCvByIdIsNullException;
import com.zpark.resume.exception.UpdateExperienceLoseException;
import com.zpark.resume.mapper.IExperienceMapper;
import com.zpark.resume.service.IExperienceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @PackageName: com.zpark.resume.service.impl
 * @ClassName: ExperienceServiceImpl
 * @Description:
 * @author: RZS
 * @date: 2023/6/1  18:08
 */
@Service
public class ExperienceServiceImpl implements IExperienceService {

    @Autowired
    private IExperienceMapper experienceMapper;

    @Override
    public List<Experience> findByCvId(Integer cvId) {
        // 调用持久层接口查询数据
        List<Experience> lists = experienceMapper.selectByCvId(cvId);
        // 判断查询结果
        if (lists.size() <= 0) {
            // 结果为空，抛出异常FindCvByIdIsNullException("无效的ID")
            throw new FindCvByIdIsNullException("无效的ID");
        }
        return lists;
    }

    /**
     * 添加工作经历
     * @param experience
     * @return
     */
    @Override
    public Integer addExperience(Experience experience) {
        // 判断experience是否为null
        if (experience == null) {
            // 如果为null，则抛出异常
            throw new AddExperienceLoseException("添加工作经历失败，原因未知");
        }

        // 调用持久层接口执行添加数据操作
        Integer row = experienceMapper.insertExperience(experience);
        // 判断执行结果是否成功
        if (row <= 0) {
            throw new AddExperienceLoseException("添加工作经历失败，原因未知");
        }
        // 返回受影响到行数
        return row;
    }

    /**
     * 修改工作经历数据
     * @param experience
     * @return
     */
    @Override
    public Integer changeExperience(Experience experience) {
        // 判断experience是否为null
        if (experience == null) {
            throw new UpdateExperienceLoseException("修改工作经历失败异常");
        }
        // 调用持久层接口实现数据修改
        Integer row = experienceMapper.updateExperienceById(experience);
        // 判断修改结果
        if (row <= 0) {
            throw new UpdateExperienceLoseException("修改工作经历失败异常");
        }
        // 返回首影响的行数
        return row;
    }

    /**
     * 删除工作经历数据
     * @param id
     * @return
     */
    @Override
    public Integer removeExperience(Integer id) {
        // 判断experience是否为null
        if (id == null || id == 0) {
            throw new DeleteExperienceLoseException("修改工作经历失败异常");
        }
        // 调用持久层接口实现数据修改
        Integer row = experienceMapper.deleteExperienceById(id);
        // 判断修改结果
        if (row <= 0) {
            throw new DeleteExperienceLoseException("修改工作经历失败异常");
        }
        // 返回首影响的行数
        return row;
    }
}
